package com.myprinciple.ocp.improve;


/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/4/10 9:51
 */
public class OCP {

    public static void main(String[] args) {
        // 使用看看存在的问题
        GraphEditor graphEditor = new GraphEditor();
        graphEditor.drawShape(new Rectangle());
        graphEditor.drawShape(new Circle());
        graphEditor.drawShape(new Triangle());
        graphEditor.drawShape(new OtherGraphic());

    }
    
}

// 这是一个用于绘图的类[使用方]
class GraphEditor {
    
    // 接收Shape对象，调用draw方法 
    public void drawShape(Shape s){
        s.draw();
    }
    
}

// Shape类，基类
abstract class Shape {
    int mType;
    public abstract void draw();
}
// 矩形类
class Rectangle extends Shape {
    public Rectangle() {
        super.mType = 1;
    }

    @Override
    public void draw() {
        System.out.println("绘制矩形");
    }
}

// 圆形类
class Circle extends Shape {
    public Circle() {
        super.mType = 2;
    }

    @Override
    public void draw() {
        System.out.println("绘制圆形");
    }
}

// 新增三角形
class Triangle extends Shape{
    public Triangle(){
        super.mType = 3;
    }

    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}

// 新增一个图形
class OtherGraphic extends Shape{

    public OtherGraphic(){
        super.mType = 4;
    }
    
    @Override
    public void draw() {
        System.out.println("绘制其它图形");
    }
}
